/**
 * 劳动力人员信息
 */ 
var orgData = _appConfigs.getUserData().userOrgs;
//var userRole = _appConfigs.getUserData().getRoles();
//alert(userRole);
$(function(){
	var tableNames = ["dict_gender","dict_education"];
	for(var i=0;i<tableNames.length;i++){
		var id="#laborAdd_"+tableNames[i];
		$(id).combobox({
			data:$.appCache.get(tableNames[i]),
			valueField:'code',    
		    textField:'name',
		    //required:true,
		    editable:false,
		    panelMaxHeight:90
		});
	}
	
	for(var j=0;j<tableNames.length;j++){
		var id1="#laborUpdate_"+tableNames[j];
		$(id1).combobox({
			data:$.appCache.get(tableNames[j]),
			valueField:'code',    
		    textField:'name',
		    //required:true,
		    editable:false,
		    panelMaxHeight:90
		});
	}
	var employestateArr=['#laborAdd_dict_employestate','#laborUpdate_dict_employestate'];
	for(var m=0;m<employestateArr.length;m++){
		$(employestateArr[m]).combobox({
			data:$.appCache.get('dict_employestate'),
			valueField:'code',    
		    textField:'name',
		    required:true,
		    missingMessage:'必填',
		    editable:false,
		    panelMaxHeight:90
		});
	}
	
	var streetArr = ['#laborAdd_dict_street','#laborUpdate_dict_street','#LaborSearch_street'];
	for(var i=0;i<streetArr.length;i++){
		$(streetArr[i]).combobox({
			data:$.appCache.get("dict_street"),
			valueField:'code',    
		    textField:'name',
		    missingMessage:'必填',
		    editable:false,
		    panelMaxHeight:90
		});
	}
	
	$("#laborAdd_dict_street").combobox({
		required:true,
	    onSelect: function(record){
	    	$("#laborAdd_dict_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    required:true,
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	
	$("#laborUpdate_dict_street").combobox({
		required:true,
		onSelect: function(record){
		    	$("#laborUpdate_dict_community").combobox({
		    		url:'application/dict/getSubMenu',
		    		queryParams:{
		    			tableName:'dict_community',
		    			parent_code:record.code
		    		},
		    		valueField:'code',    
				    textField:'name',
				    required:true,
				    missingMessage:'必填',
				    editable:false,
				    panelMaxHeight:90
		    	}); 
		    }
	});
	
	$("#LaborSearch_street").combobox({
	    onSelect: function(record){
	    	$("#LaborSearch_community").combobox({
	    		url:'application/dict/getSubMenu',
	    		queryParams:{
	    			tableName:'dict_community',
	    			parent_code:record.code
	    		},
	    		valueField:'code',    
			    textField:'name',
			    missingMessage:'必填',
			    editable:false,
			    panelMaxHeight:90
	    	}); 
	    }
	});
	
	$("#addLaborInfo").window({
		onClose:function(data){
			$('#addLaborInfoForm').form('clear');
		}  		      
	});
	
});
/**
 * 默认选街道
 */
if(orgData.porgId == _appConfigs.appOrgId){ //街道
	$('#LaborSearch_street').combobox({disabled:true});
	$('#LaborSearch_street').combobox('select',orgData.orgId);
	$('#laborUpdate_dict_street').combobox({readonly:true});
	$('#laborUpdate_dict_street').combobox('select',orgData.orgId);
	$('#importLaborButton').linkbutton('enable');
}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){	//社区
	$('#LaborSearch_street').combobox({disabled:true});
	$('#LaborSearch_community').combobox({disabled:true});
	$('#LaborSearch_street').combobox('select',orgData.porgId);
	$('#LaborSearch_community').combobox('select',orgData.orgId);
	$('#laborUpdate_dict_street').combobox({readonly:true});
	$('#laborUpdate_dict_community').combobox({readonly:true});
	$('#laborUpdate_dict_street').combobox('select',orgData.porgId);
	$('#laborUpdate_dict_community').combobox('select',orgData.orgId);
	$('#importLaborButton').linkbutton('disable');
}else{ //西城区
	$("#LaborSearch_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    panelMaxHeight:90
	}); 
	$("#laborUpdate_dict_community").combobox({
		data:$.appCache.get('dict_community'),
		valueField:'code',    
	    textField:'name',
	    missingMessage:'必填',
	    editable:false,
	    required:true,
	    panelMaxHeight:90
	});
	$('#importLaborButton').linkbutton('disable');
}
$('#laborInfoGrid').datagrid({
	url:'labor/management/getLaborInfoList', 
	queryParams:{
		street: $('#LaborSearch_street').combobox('getValue'),
		community: $('#LaborSearch_community').combobox('getValue')
	},
    idField:'laborinfoId',
    rownumbers:true,
    fitColumns:true,
    singleSelect:true,
    pagination:true,
    columns:[[ 
        {field:'laborinfoId',title:'主键',hidden:true},
        {field:'name',title:'姓名',width:90,align:'center'},    
        {field:'sex',title:'性别',width:80,align:'center',
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_gender');
        		   if(!dict|| !dict.length) {
        			   return value;
        		   } else {
        			   var i = 0;
            		   for(i; i<dict.length; i++) {
            			   var data = dict[i];
            			   if(value == data.code) {
            				   return data.name;
            			   }
            		   }
        		   }
        	   }},  
        {field:'idcard',title:'身份证号',width:125,align:'center'},   
        {field:'education',title:'学历',width:100,align:'center',
        	   formatter:function(value){
     		       var dict = $.appCache.get('dict_education');
     		       if(!dict || !dict.length) {
     		    	   return value;
     		       } else {
     			       var i = 0;
         		       for(i; i<dict.length; i++) {
         		    	   var data = dict[i];
         			       if(value == data.code) {
         			    	   return data.name;
         			       }
         		      }
     		       }
     	       }},  
        {field:'phone',title:'联系方式',width:100,align:'center'},    
        {field:'status',title:'就业状态',width:100,align:'center', 
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_employestate');
             	   if(!dict || !dict.length) {
             		   return value;
             	   } else {
             		   var i = 0;
                 	   for(i; i<dict.length; i++) {
                 		   var data = dict[i];
                 		   if(value == data.code) {
                 			   return data.name;
                 		   }
                 	   }
             	   }
               }},  
        {field:'company',title:'所在单位',width:120,align:'center'},    
        {field:'unemploymentdate',title:'失业时间',width:100,align:'center',
        	   formatter:function(data){ //EasyUI的DataGrid日期列(datebox)正确显示json时间格式 
        		   if(data!=null){
        			   var df = new Date(data);  
                       return df.format("yyyy-MM-dd");       			   
        		   } else{
        			   return null;
        		   }
        	   }},  
        {field:'street',title:'街道',width:100,align:'center', 
        	   formatter:function(value){
        		   var dict = $.appCache.get('dict_street');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                     	   if(value == data.code) {
                     		   return data.name;
                     	   }
                       }
                   }
               }},  
        {field:'community',title:'社区',width:120,align:'center',
               formatter:function(value){
            	   var dict = $.appCache.get('dict_community');
                   if(!dict || !dict.length) {
                	   return value;
                   } else {
                	   var i = 0;
                       for(i; i<dict.length; i++) {
                    	   var data = dict[i];
                           if(value == data.code) {
                        	   return data.name;
                           }
                       }
                    }
               }},  
        {field:'remark',title:'备注',width:100,align:'center'}
    ]]
}); 
 
/**
 * 查询条件
 */
function laborSearch(){
	$('#laborInfoGrid').datagrid('load',{
		street: $('#LaborSearch_street').combobox('getValue'),
		community: $('#LaborSearch_community').combobox('getValue'),
		name: $('#LaborSearch_name').val(),	
		idcard: $('#LaborSearch_idCard').val() 					
	});
	$('#laborInfoGrid').datagrid('unselectAll');
}

function addLaborData(){
	if(orgData.porgId == _appConfigs.appOrgId){
		$('#laborAdd_dict_street').combobox({readonly:true});
		//$('##laborAdd_dict_street').combobox({disabled:true});
		$('#laborAdd_dict_street').combobox('select',orgData.orgId);
	}else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){
	    $('#laborAdd_dict_street').combobox({readonly:true});
		//$('#laborAdd_dict_street').combobox({disabled:true});
		$('#laborAdd_dict_community').combobox({readonly:true});
		//$('#laborAdd_dict_community').combobox({disabled:true});
		$('#laborAdd_dict_street').combobox('select',orgData.porgId);
		$('#laborAdd_dict_community').combobox('select',orgData.orgId);
	}else{
		$("#laborAdd_dict_community").combobox({
			data:$.appCache.get('dict_community'),
			valueField:'code',    
		    textField:'name',
		    missingMessage:'必填',
		    editable:false,
		    required:true,
		    panelMaxHeight:90
		}); 
	}
	
	$('#laborAdd_dict_employestate').combobox({
		onSelect: function(record){
			//alert(record.code);
			if(record.code == '5'){
				var curr_time = new Date();
				var strDate = curr_time.getFullYear()+"-"+(curr_time.getMonth()+1)+"-"+curr_time.getDate();
				$('#laborAdd_unemploymentdate').datebox('setValue',strDate);
			} else{
				$('#laborAdd_unemploymentdate').datebox('setValue','');
			}
		}
	});

	$('#addLaborInfo').window('open');
}

/**
 * 添加劳动力信息
 */
function insertLaborInfo(){
//	$('#addLaborInfoForm').form('submit',{
//		url:'labor/management/insertLaborInfo',
//		success: function(data){
//			data =  $.parseJSON(data);
//			if(data.success == 'true'){
//				$.messager.alert('提示','保存成功','info');
//				$('#addLaborInfo').window('close');
//				$('#addLaborInfoForm').form('clear');
//				laborSearch();
//			}else{
//				$.messager.alert('提示',data.message,'info');
//			}
//		}
//	});
	
	var laborFlag = $('#addLaborInfoForm').form('validate');
	if(laborFlag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'labor/management/insertLaborInfo',
		    type:'post',
		    async: false,
		    data:$('#addLaborInfoForm').serialize(),
		    success:function(data){
		    	data =  $.parseJSON(data);
			    if(data.success == 'true'){
			    	$.messager.alert('提示','保存成功','info');
				    $('#addLaborInfo').window('close');
				    $('#addLaborInfoForm').form('clear');
				    laborSearch();
			    }else{
			    	$.messager.alert('提示',data.message,'info');
			    }
		    },
		    error:function(data){
		    	alert("ajax发生错误！");	
		    }
	    });
	}
}

/**
 * 导入劳动力数据
 */
function importLabor(){
	$('#uploadDataForm').form('submit',{
		url:'labor/management/importLaborData',
		success: function(data){
			data =  $.parseJSON(data);
			if(data.success == 'true'){
				$.messager.alert('提示','数据上传成功','info');
				$('#importLaborInfo').window('close');
				$('#uploadDataForm').form('clear');
				laborSearch();
			}else{
				$.messager.alert('提示',data.message,'info');
			}
		}
	});
}

/**
 * 回显劳动力信息
 */
function getLaborInfo(){
	var rowData = $('#laborInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$('#modifyLaborInfo').window('open');
		$.ajax({
				url:'labor/management/getLaborInfo',
				type:'post',
				data:{
					//根据传回的id值查询该条记录
					laborinfoId:rowData.laborinfoId					
				},
				success: function (data) { 
					if(data==null){
						$('#modifyLaborInfoForm').form('clear');
					}else{
						$('#modifyLaborInfoForm').form('load',data);
					}
					
		        },
		        error:function(data){
		        	$('#modifyLaborInfoForm').form('clear');
		        }
			});
		}
}

/**
 * 修改劳动力信息
 * 
 */
function modifyLaborInfo(){
//	$('#modifyLaborInfoForm').form('submit',{
//		url:'labor/management/updateLaborInfo',
//		success: function(data){
//			data =  $.parseJSON(data);
//			if(data.success == 'true'){
//				$.messager.alert('提示','修改成功','info');
//				$('#modifyLaborInfo').window('close');
//				$('#modifyLaborInfoForm').form('clear');
//				laborSearch();
//			}else{
//				$.messager.alert('提示',data.message,'info');
//				$('#modifyLaborInfo').window('close');
//				$('#modifyLaborInfoForm').form('clear');
//			}
//		}
//	});
	
	var updateFlag = $('#modifyLaborInfoForm').form('validate');
	if(updateFlag==false){
		$.messager.alert('提示','表单填写不正确');
		return;
	}else{
		$.ajax({
			url:'labor/management/updateLaborInfo',
			type:'post',
	        async: false,
	        data:$('#modifyLaborInfoForm').serialize(),
	        success:function(data){
	        	data =  $.parseJSON(data);
		        if(data.success == 'true'){
		        	$.messager.alert('提示','保存成功','info');
			        $('#modifyLaborInfo').window('close');
			        $('#modifyLaborInfoForm').form('clear');
			        laborSearch();
		        }else{
		        	$.messager.alert('提示',data.message,'info');

		        }
	        },
	        error:function(data){
	        	alert("ajax发生错误！");	
	        }
        });
	}
}

/**
 * 删除一条劳动力信息记录
 * 
 */
function deleteLaborInfo(){
	var rowData = $('#laborInfoGrid').datagrid('getSelected');
	if(rowData == undefined){
		$.messager.alert('消息','请选择一条数据');
	}else{
		$.messager.confirm('提示','确定要删除吗？',function(r){
			if(r){
				$.ajax({
					url:'labor/management/deleteLaborInfo',
					type:'post',
					data:{
						laborinfoId:rowData.laborinfoId
					},
					success:function(data){
						data =  $.parseJSON(data);
						if(data.success == 'true'){
							$.messager.alert('提示','删除成功','info');
							laborSearch();
						}else{
							$.messager.alert('提示',data.message,'info');
						}
					}
				});
			}
		});
	}
}

function exportData(){
	 var street,community;
	 if(orgData.porgId == _appConfigs.appOrgId){  //街道
		 street = orgData.orgId;
		 community = "";
	 }else if(orgData.porgId != _appConfigs.appOrgId && orgData.orgName.indexOf('社区')>=0){ //社区
		 street = "";
		 community = orgData.orgId;
	 }else{  //全部
		 street = "";
		 community = "";
	 }
	 window.location.href ="labor/excel/exportLaborInfo?street="+street+"&community="+community;
}
/**
 * 日期格式化
 * @param value
 * @param row
 * @returns {String}
 */
function formatterDate(value, row) {
    var date = new Date(value);
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
}


